package chalmers.game.subterranean.ui.view;

import chalmers.game.subterranean.event.Bus;
import chalmers.game.subterranean.event.events.minors.MoveMinorEvent;
import chalmers.game.subterranean.event.events.minors.SpawnMinorEvent;
import chalmers.game.subterranean.util.options.Sprites;
import chalmers.game.subterranean.util.options.WorldValues;
import com.google.common.eventbus.Subscribe;
import java.util.HashMap;
import java.util.Map;
import javafx.animation.TranslateTransition;
import javafx.application.Platform;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.util.Duration;

/**
 * 
 * @author Alex Evert
 */
public class Minors {

  private final Map<Integer, TranslateTransition> minors = new HashMap<>();
  private final Pane world;


  public Minors(Pane world) {
    this.world = world;
    Bus.INSTANCE.register(this);
  }


  @Subscribe
  public void move(MoveMinorEvent event) {
    TranslateTransition mover = minors.get(event.hash);
    mover.setToX(event.pos.getX() * WorldValues.VIEW_TO_MODEL_SCALE - WorldValues.BLOCK_SIZE / 2);
    mover.setToY(event.pos.getY() * WorldValues.VIEW_TO_MODEL_SCALE + event.pos.getDepth()
        * WorldValues.LEVEL_SIZE);
    mover.play();
  }

  @Subscribe
  public void spawn(SpawnMinorEvent event) {
	TranslateTransition mover = new TranslateTransition();
	mover.setDuration(Duration.ONE);
    ImageView img = new ImageView(Sprites.PLAYER_SPRITE);
	Platform.runLater(
		() -> { world.getChildren().add(img);}
	);
	mover.setNode(img);
    minors.put(event.hash, mover);
  }
}
